<template>
  <div>
    <el-form
      ref="form"
      :rules="rules"
      :model="form"
      label-width="100px"
      style="width: 500px"
    >
      <el-form-item label="房间名称" prop="rname">
        <el-input v-model="form.rname" />
      </el-form-item>
      <el-form-item prop="area" label="房间大小">
        <el-input v-model="form.area">
          <template slot="append">平方米</template>
        </el-input>
      </el-form-item>
      <el-form-item prop="bedtype" label="床类型">
        <el-select v-model="form.bedtype" placeholder="请选择">
          <el-option value="大床">大床</el-option>
          <el-option value="双床">双床</el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="breakfast" label="是否含早餐">
        <template>
          <el-radio v-model="form.breakfast" label="含早餐">含早餐</el-radio>
          <el-radio v-model="form.breakfast" label="不含早餐"
            >不含早餐</el-radio
          >
        </template>
      </el-form-item>
      <el-form-item prop="window" label="是否有窗">
        <template>
          <el-radio v-model="form.window" label="有窗">有窗</el-radio>
          <el-radio v-model="form.window" label="无窗">无窗</el-radio>
        </template>
      </el-form-item>

      <el-form-item prop="rcount" label="房间数量">
        <el-input v-model="form.rcount">
          <template slot="append">间</template>
        </el-input>
      </el-form-item>
      <el-form-item prop="rprice" label="房间价格">
        <el-input v-model="form.rprice">
          <template slot="append">元/天</template>
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">立即修改</el-button>
        <el-button type="primary" @click="$router.go(-1)">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import myaxios from '@/http/MyAxios'

export default {
  data() {
    return {
      form: {
        rname: '',
        area: '',
        bedtype: '',
        breakfast: '',
        window: '',
        rcount: '',
        rprice: '',
        rid: '',
      },
      rules: {
        // 整理表单验证规则
        rname: [{ required: true, message: '必填', trigger: 'blur' }],
        area: [{ required: true, message: '必填', trigger: 'blur' }],
        bedtype: [{ required: true, message: '必填', trigger: 'blur' }],
        breakfast: [{ required: true, message: '必填', trigger: 'blur' }],
        window: [{ required: true, message: '必填', trigger: 'blur' }],
        rcount: [{ required: true, message: '必填', trigger: 'blur' }],
        rprice: [{ required: true, message: '必填', trigger: 'blur' }],
      },
    }
  },
  methods: {
    submit() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          this.$http.roomApi.updateroom(this.form).then(res => {
            console.log(res)
            if (res.data.code == 200) {
              this.$message({ type: 'success', message: '修改成功' })
              this.$router.go(-1)
              this.form = ''
            }
          })
        }
      })
    },
  },
  mounted() {
    let params = this.$route.params.rid
    console.log(params)
    myaxios
      .get(`http://127.0.0.1:3000/room/infor/query?rid=${params}`)
      .then(res => {
        console.log(res)
        this.form = res.data.data[0]
      })
  },
}
</script>

<style lang="scss" scoped></style>
